﻿#region Namespaces

using System;
using System.IO;

#endregion

namespace Vibstudio.X4NET.Csv
{
    public static class CsvExtensions
    {
        public static string[] ToRows(this byte[] fileBytes)
        {
            string[] csvRows;
            using (Stream stream = new MemoryStream(fileBytes))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string csv = reader.ReadToEnd();
                    csvRows = csv.Split('\n');
                    for (int i = 0; i < csvRows.Length; i++)
                    {
                        csvRows[i] = csvRows[i].RemoveNewlineAndLineFeed();
                    }
                }
            }
            return csvRows;
        }

        private static string RemoveNewlineAndLineFeed(this string s)
        {
            string[] lf = { "\r", "\n" };
            return string.Join("", s.Split(lf, StringSplitOptions.RemoveEmptyEntries));
        }
    }
}